#include "global.h"
#include "gb.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <atomic>

bool                    g_sip_init = false;
CMkThread               g_udp_event_thread[2];
Uint32                  g_udp_event_flag = 0;
CMkThread               g_tcp_event_thread[2];
Uint32                  g_tcp_event_flag = 0;
char *                  g_siphost = NULL;
char *                  g_sipid = NULL;
char *                  g_realm = NULL;
Uint32                  g_sipport = 0;
Uint32                  g_wait_time = 0;
fexception_callback     g_excep_cb = NULL;
void *                  g_excep_user = NULL;
Uint8                   g_authorizate = 1;

std::vector<mk_gb_server_param_t>    g_gb_server_list;

std::atomic<Uint32> g_message_sn(1);


void mk_gbtime_to_sdktime(const char *gbtime, lpnet_time sdktime)
{
    memset(sdktime, 0, sizeof(mk_net_time_t));
    char tmp[LEN_256] = { 0 };
    const char *year_end;
    const char *month_end;
    const char *day_end;
    const char *hour_end;
    const char *min_end;

    //year
    year_end = strchr(gbtime, '-');
    if(!year_end || year_end - gbtime >= sizeof(tmp))
        return;
    memcpy(tmp, gbtime, year_end - gbtime);
    sdktime->year = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //month
    month_end = strchr(year_end + 1, '-');
    if(!month_end || month_end - year_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, year_end + 1, month_end - year_end - 1);
    sdktime->month = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //day
    day_end = strchr(month_end + 1, 'T');
    if(!day_end || day_end - month_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, month_end + 1, day_end - month_end - 1);
    sdktime->day = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //hour
    hour_end = strchr(day_end + 1, ':');
    if(!hour_end || hour_end - day_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, day_end + 1, hour_end - day_end - 1);
    sdktime->hour = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //min
    min_end = strchr(hour_end + 1, ':');
    if(!min_end || min_end - hour_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, hour_end + 1, min_end - hour_end - 1);
    sdktime->minute = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //sec
    sdktime->second = atoi(min_end + 1);
}

void mk_sdktime_to_gbtime(const lpnet_time sdktime, char *gbtime)
{
    snprintf(gbtime, 50, "%04d-%02d-%02dT%02d:%02d:%02d", sdktime->year, sdktime->month, sdktime->day, sdktime->hour,
        sdktime->minute, sdktime->second);
}

void mk_gbtime_to_systime(const lpnet_time sdktime, char *gbtime)
{
    snprintf(gbtime, 50, "%04d-%02d-%02dT%02d:%02d:%02d", sdktime->year + 1900, sdktime->month + 1, sdktime->day, sdktime->hour,
             sdktime->minute, sdktime->second);
}

void mk_sdktime_to_systemtime(const char *stime, struct tm *st)
{
    char tmp[LEN_256] = { 0 };
    const char *year_end;
    const char *month_end;
    const char *day_end;
    const char *hour_end;
    const char *min_end;
    memset(st, 0, sizeof(struct tm));

    //year
    year_end = strchr(stime, '-');
    if(!year_end || year_end - stime >= sizeof(tmp))
        return;
    memcpy(tmp, stime, year_end - stime);
    st->tm_year = atoi(tmp) - 1900;
    memset(tmp, 0, LEN_256);

    //month
    month_end = strchr(year_end + 1, '-');
    if(!month_end || month_end - year_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, year_end + 1, month_end - year_end - 1);
    st->tm_mon = atoi(tmp) - 1;
    memset(tmp, 0, LEN_256);

    //day
    day_end = strchr(month_end + 1, 'T');
    if(!day_end || day_end - month_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, month_end + 1, day_end - month_end - 1);
    st->tm_mday = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //hour
    hour_end = strchr(day_end + 1, ':');
    if(!hour_end || hour_end - day_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, day_end + 1, hour_end - day_end - 1);
    st->tm_hour = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //min
    min_end = strchr(hour_end + 1, ':');
    if(!min_end || min_end - hour_end - 1 >= sizeof(tmp))
        return;
    memcpy(tmp, hour_end + 1, min_end - hour_end - 1);
    st->tm_min = atoi(tmp);
    memset(tmp, 0, LEN_256);

    //sec
    st->tm_sec = atoi(min_end + 1);

}

void mk_sdktime_to_utctime(const lpnet_time sdktime, time_t& utctime)
{
    struct tm TmTime;
    TmTime.tm_year = sdktime->year - 1900;
    TmTime.tm_mon = sdktime->month - 1;
    TmTime.tm_mday = sdktime->day;
    TmTime.tm_hour = sdktime->hour;
    TmTime.tm_min = sdktime->minute;
    TmTime.tm_sec = sdktime->second;
    utctime = mktime(&TmTime);
}

Uint32 get_message_sn()
{
    g_message_sn += 1;
    return g_message_sn;
}



